import axios from "../../utils/axios"

// pages/changeplan/changeplan.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    planmodes:["学习","娱乐","交友"],
    remindmodes:["从不","每天","每周","每月"],
    currentindex:1,
    plandetail:{}
  },
  getplandetail:function(e){
    var plandetail=this.data.plandetail
    if(e.type=='input'){
      if(e.currentTarget.id=='planname'){
        plandetail.name=e.detail.value
      }else{
        plandetail.remarks=e.detail.value
      }
    }else{
      if(e.type=='tap'){
        this.setData({
          currentindex:e.currentTarget.dataset.index
        })
        plandetail.mode=this.data.planmodes[e.currentTarget.dataset.index]
      }else{
        if(e.currentTarget.id=="date"){
         plandetail.date=e.detail.value
        }else{
          if(e.currentTarget.id=="time"){
            plandetail.time=e.detail.value
          }else{
            if(e.currentTarget.id=="remindmode"){
              plandetail.remindmode=this.data.remindmodes[e.detail.value]
            }else{
              if(e.currentTarget.id=="isremind"){
                plandetail.mind=!plandetail.mind
              }else{
                plandetail.top=!plandetail.top
              }
            }
          }
        }
      }
    }
    this.setData({
      plandetail:plandetail
    })
    console.log(plandetail)
  },
 async save(){
    var plandetail=this.data.plandetail
    var tiemarr=plandetail.time.split(":")
    var cron=0+" "+tiemarr[1]+" "+tiemarr[0]
    var remindmode=plandetail.remindmode
    var date=plandetail.date
    var datearr=date.split("-")
    switch (remindmode){
      case "从不": 
      cron=cron+" "+datearr[2]+" "+datearr[1]+" ? *"
      break
      case "每天": 
      cron=cron+" * * ? *"
      break;
      case "每周":
      cron=cron+" "+datearr[2]+"/7"+" "+datearr[1]+" ?"
      break;
      case "每月":
      cron=cron+" "+datearr[2]+" * ?"
    }
    cron+=" "+new Date().getFullYear()
    console.log(cron)
      var that=this
    wx.showModal({
      title:"是否确认修改",
      success:function(res){
        if(res.confirm){
          var token=getApp().globalData.accessToken
          console.log(token)
          var data={
            isAllDay:true,
              cron:cron,
              isMind:plandetail.mind,
              isTop:plandetail.top,
              mode:that.data.planmodes[that.data.currentindex],
              name:plandetail.name,
              remarks:plandetail.remarks,
              id:plandetail.id
          }
         axios.patch('/plan',data)
        wx.showToast({
          title:"修改成功"
        })
        setTimeout(() => {
          wx.switchTab({
            url:"../calendar/calendar"
          })
        }, 500);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
   var plandetail=JSON.parse(option.plandetail)
   console.log(plandetail)
   var currentindex=0
  switch(plandetail.mode){
    case "娱乐" :currentindex=1
    break
    case "学习" :currentindex=0
    break
    case "交友" :currentindex=2
  }
   this.setData({
     currentindex:currentindex,
     plandetail:plandetail
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})